package com.amt.configure;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**   
 * @Title: RedisConfig.java 
 * @Package com.amt.configure 
 * @Description: TODO(Redis配置类) 
 * @author 江伟  
 * @date 2020年12月14日 下午3:21:47
 * Copyright (c) ©1994-2021 Scjydz.com All Rights Reserved.
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
	
	/**
	 * @description: 设置Redis模版
	 * @param factory
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		// 设置序列化工具
		setSerializer(redisTemplate);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

	/**
	 * @description: 设置序列化
	 * @param template
	 */
	private void setSerializer(RedisTemplate<String, Object> template) {
		@SuppressWarnings("rawtypes")
		RedisSerializer stringSerializer = new StringRedisSerializer();
		template.setValueSerializer(stringSerializer);
		template.setKeySerializer(stringSerializer);
		template.setHashKeySerializer(stringSerializer);
		template.setHashValueSerializer(stringSerializer);
	}

}
